//
//  ByNetImageApi.m
//  beibei_ios
//
//  Created by pro on 16/5/19.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "ByNetImageApi.h"

#import "ByNetCarouselModel.h"

@implementation ByNetImageApi

#pragma mark - 获得轮播图
+ (void)imageCarouselGet:(ByNetParamsModel *)model
                 success:(void(^)(ByNetBaseResult *result))success
                 failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_Banners_query";
    static NSInteger apiVersion = 100;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.position forKey:@"position"];
    [params setObject:model.page_num forKey:@"page_num"];
    [params setObject:model.page_size forKey:@"page_size"];

    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeDictionary
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
                      
           DLog(@"******** 轮播图获取 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"轮播图获取成功";
           
           NSMutableArray *retArray = [[NSMutableArray alloc] init];
           if ([responseObject isKindOfClass:[NSDictionary class]]) {
               NSArray *results = responseObject[@"result"];
               for (NSDictionary *dic in results) {
                   ByNetCarouselModel *model = [[ByNetCarouselModel alloc] initContentWithDic:dic];
                   [retArray addObject:model];
               }
           }
           result.data = [retArray copy];
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           DLog(@"******** 轮播图获取 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert alertError:result.msg];
           }
       }];
}
@end
